<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover"/>
<meta name="description" content="Animating diagram parts to travel along link paths from one node to another."/> 
<link rel="stylesheet" href="../assets/css/style.css"/> 
<!-- Copyright 1998-2021 by Northwoods Software Corporation. -->
<title>Concept Map with animation along paths</title>
</head>

<body>
  <!-- This top nav is not part of the sample code -->
  <nav id="navTop" class="w-full z-30 top-0 text-white bg-nwoods-primary">
    <div class="w-full container max-w-screen-lg mx-auto flex flex-wrap sm:flex-nowrap items-center justify-between mt-0 py-2">
      <div class="md:pl-4">
        <a class="text-white hover:text-white no-underline hover:no-underline
        font-bold text-2xl lg:text-4xl rounded-lg hover:bg-nwoods-secondary " href="../">
          <h1 class="mb-0 p-1 ">GoJS</h1>
        </a>
      </div>
      <button id="topnavButton" class="rounded-lg sm:hidden focus:outline-none focus:ring" aria-label="Navigation">
        <svg fill="currentColor" viewBox="0 0 20 20" class="w-6 h-6">
          <path id="topnavOpen" fill-rule="evenodd" d="M3 5a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zM3 10a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zM9 15a1 1 0 011-1h6a1 1 0 110 2h-6a1 1 0 01-1-1z" clip-rule="evenodd"></path>
          <path id="topnavClosed" class="hidden" fill-rule="evenodd" d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z" clip-rule="evenodd"></path>
        </svg>
      </button>
      <div id="topnavList" class="hidden lg:text-base sm:block items-center w-auto mt-0 text-white p-0 z-20">
        <ul class="list-reset list-none font-semibold flex justify-end flex-wrap sm:flex-nowrap items-center px-0 pb-0">
          <li class="p-1 sm:p-0"><a class="topnav-link" href="../learn/">Learn</a></li>
          <li class="p-1 sm:p-0"><a class="topnav-link" href="../samples/">Samples</a></li>
          <li class="p-1 sm:p-0"><a class="topnav-link" href="../intro/">Intro</a></li>
          <li class="p-1 sm:p-0"><a class="topnav-link" href="../api/">API</a></li>
          <li class="p-1 sm:p-0"><a class="topnav-link" href="https://www.nwoods.com/products/register.html">Register</a></li>
          <li class="p-1 sm:p-0"><a class="topnav-link" href="../download.html">Download</a></li>
          <li class="p-1 sm:p-0"><a class="topnav-link" href="https://forum.nwoods.com/c/gojs/11">Forum</a></li>
          <li class="p-1 sm:p-0"><a class="topnav-link" href="https://www.nwoods.com/contact.html"
           target="_blank" rel="noopener" onclick="getOutboundLink('https://www.nwoods.com/contact.html', 'contact');">Contact</a></li>
          <li class="p-1 sm:p-0"><a class="topnav-link" href="https://www.nwoods.com/sales/index.html"
           target="_blank" rel="noopener" onclick="getOutboundLink('https://www.nwoods.com/sales/index.html', 'buy');">Buy</a></li>
        </ul>
      </div>
    </div>
    <hr class="border-b border-gray-600 opacity-50 my-0 py-0" />
  </nav>
  <div class="md:flex flex-col md:flex-row md:min-h-screen w-full max-w-screen-xl mx-auto">
    <div id="navSide" class="flex flex-col w-full md:w-48 text-gray-700 bg-white flex-shrink-0"></div>
    <!-- * * * * * * * * * * * * * -->
    <!-- Start of GoJS sample code -->
    
    <script src="../release/go.js"></script>
    <div class="p-4 w-full">
    <script id="code">
    function init() {
      var $ = go.GraphObject.make;  // for conciseness in defining templates

      myDiagram =
        $(go.Diagram, "myDiagramDiv",  // must name or refer to the DIV HTML element
          {
            initialAutoScale: go.Diagram.Uniform,  // an initial automatic zoom-to-fit
            contentAlignment: go.Spot.Center,  // align document to the center of the viewport
            layout:
              $(go.ForceDirectedLayout,  // automatically spread nodes apart
                { defaultSpringLength: 30, defaultElectricalCharge: 100 }),
            "undoManager.isEnabled": true
          });

      // define each Node's appearance
      myDiagram.nodeTemplate =
        $(go.Node, "Auto",  // the whole node panel
          { locationSpot: go.Spot.Center },
          // define the node's outer shape, which will surround the TextBlock
          $(go.Shape, "Rectangle",
            { fill: $(go.Brush, "Linear", { 0: "rgb(254, 201, 0)", 1: "rgb(254, 162, 0)" }), stroke: "black" }),
          $(go.TextBlock,
            { font: "bold 10pt helvetica, bold arial, sans-serif", margin: new go.Margin(4, 4, 3, 20) },
            new go.Binding("text", "text"))
        );

      // replace the default Link template in the linkTemplateMap
      myDiagram.linkTemplate =
        $(go.Link,  // the whole link panel
          // path animation works with these kinds of links too:
          //{ routing: go.Link.AvoidsNodes },
          //{ curve: go.Link.Bezier },
          $(go.Shape,  // the link shape
            { stroke: "black" }),
          $(go.Shape,  // the arrowhead
            { toArrow: "standard", stroke: null }),
          $(go.Panel, "Auto",
            $(go.Shape,  // the label background, which becomes transparent around the edges
              {
                fill: $(go.Brush, "Radial", { 0: "rgb(240, 240, 240)", 0.3: "rgb(240, 240, 240)", 1: "rgba(240, 240, 240, 0)" }),
                stroke: null
              }),
            $(go.TextBlock,  // the label text
              {
                textAlign: "center",
                font: "10pt helvetica, arial, sans-serif",
                stroke: "#555555",
                margin: 4
              },
              new go.Binding("text", "text"))
          )
        );

      myDiagram.nodeTemplateMap.add("token",
        $(go.Part,
          { locationSpot: go.Spot.Center, layerName: "Foreground" },
          $(go.Shape, "Circle",
            { width: 12, height: 12, fill: "green", strokeWidth: 0 },
            new go.Binding("fill", "color"))
        ));

      // create the model for the concept map
      var nodeDataArray = [
        { key: 1, text: "Concept Maps" },
        { key: 2, text: "Organized Knowledge" },
        { key: 3, text: "Context Dependent" },
        { key: 4, text: "Concepts" },
        { key: 5, text: "Propositions" },
        { key: 6, text: "Associated Feelings or Affect" },
        { key: 7, text: "Perceived Regularities" },
        { key: 8, text: "Labeled" },
        { key: 9, text: "Hierarchically Structured" },
        { key: 10, text: "Effective Teaching" },
        { key: 11, text: "Crosslinks" },
        { key: 12, text: "Effective Learning" },
        { key: 13, text: "Events (Happenings)" },
        { key: 14, text: "Objects (Things)" },
        { key: 15, text: "Symbols" },
        { key: 16, text: "Words" },
        { key: 17, text: "Creativity" },
        { key: 18, text: "Interrelationships" },
        { key: 19, text: "Infants" },
        { key: 20, text: "Different Map Segments" }
      ];
      var linkDataArray = [
        { from: 1, to: 2, text: "represent" },
        { from: 2, to: 3, text: "is" },
        { from: 2, to: 4, text: "is" },
        { from: 2, to: 5, text: "is" },
        { from: 2, to: 6, text: "includes" },
        { from: 2, to: 10, text: "necessary\nfor" },
        { from: 2, to: 12, text: "necessary\nfor" },
        { from: 4, to: 5, text: "combine\nto form" },
        { from: 4, to: 6, text: "include" },
        { from: 4, to: 7, text: "are" },
        { from: 4, to: 8, text: "are" },
        { from: 4, to: 9, text: "are" },
        { from: 5, to: 9, text: "are" },
        { from: 5, to: 11, text: "may be" },
        { from: 7, to: 13, text: "in" },
        { from: 7, to: 14, text: "in" },
        { from: 7, to: 19, text: "begin\nwith" },
        { from: 8, to: 15, text: "with" },
        { from: 8, to: 16, text: "with" },
        { from: 9, to: 17, text: "aids" },
        { from: 11, to: 18, text: "show" },
        { from: 12, to: 19, text: "begins\nwith" },
        { from: 17, to: 18, text: "needed\nto see" },
        { from: 18, to: 20, text: "between" }
      ];
      myDiagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);

      initTokens();
    }

    function initTokens() {
      var oldskips = myDiagram.skipsUndoManager;
      myDiagram.skipsUndoManager = true;
      myDiagram.model.addNodeDataCollection([
        { category: "token", at: 1, color: "green" },
        { category: "token", at: 2, color: "blue" },
        { category: "token", at: 4, color: "yellow" },
        { category: "token", at: 5, color: "purple" },
        { category: "token", at: 7, color: "red" },
        { category: "token", at: 8, color: "black" },
        { category: "token", at: 9, color: "green" },
        { category: "token", at: 11, color: "blue" },
        { category: "token", at: 12, color: "yellow" },
        { category: "token", at: 17, color: "purple" },
        { category: "token", at: 18, color: "red" }
      ]);
      myDiagram.skipsUndoManager = oldskips;
      window.requestAnimationFrame(updateTokens);
    }

    function updateTokens() {
      var oldskips = myDiagram.skipsUndoManager;
      myDiagram.skipsUndoManager = true;  // don't record these changes in the UndoManager!
      var temp = new go.Point();
      myDiagram.parts.each(function(token) {
        var data = token.data;
        if (!data) return;
        var at = data.at;
        if (at === undefined) return;
        var from = myDiagram.findNodeForKey(at);
        if (from === null) return;
        var frac = data.frac;
        if (frac === undefined) frac = 0.0;
        var next = data.next;
        var to = myDiagram.findNodeForKey(next);
        if (to === null) {  // nowhere to go?
          positionTokenAtNode(token, from);  // temporarily stay at the current node
          data.next = chooseDestination(token, from);  // and decide where to go next
        } else {  // proceed toward the "to" port
          var link = from.findLinksTo(to).first();
          if (link !== null) {
            token.location = link.path.getDocumentPoint(link.path.geometry.getPointAlongPath(frac, temp));
          } else {  // stay at the current node
            positionTokenAtNode(token, from);
          }
          if (frac >= 1.0) {  // if beyond the end, it's "AT" the NEXT node
            data.frac = 0.0;
            data.at = next;
            data.next = undefined;  // don't know where to go next
          } else {  // otherwise, move fractionally closer to the NEXT node
            data.frac = frac + 0.01;
          }
        }
      });
      myDiagram.skipsUndoManager = oldskips;
      window.requestAnimationFrame(updateTokens);
    }

    // determine where to position a token when it is resting at a node
    function positionTokenAtNode(token, node) {
      // these details depend on the node template
      token.location = node.position.copy().offset(4 + 6, 5 + 6);
    }

    function chooseDestination(token, node) {
      var dests = new go.List().addAll(node.findNodesOutOf());
      if (dests.count > 0) {
        var dest = dests.elt(Math.floor(Math.random() * dests.count));
        return dest.data.key;
      }
      var arr = myDiagram.model.nodeDataArray;
      // choose a random next data object that is not a token and is not the current Node
      var data = null;
      while (data = arr[Math.floor(Math.random() * arr.length)],
        data.category === "token" || data === node.data) { }
      return data.key;
    }
    window.addEventListener('DOMContentLoaded', init);
  </script>

<div id="sample">
  <div id="myDiagramDiv" style="background-color: whitesmoke; border: solid 1px black; width: 100%; height: 700px"></div>
  <p>
    This sample is copied from the <a href="conceptMap.html">Concept Map</a> sample and
    augmented with tokens that traverse the graph, stopping at nodes and travelling along links.
  </p>
  <p>
    Note that the creation and movement of the tokens is <i>not</i> recorded by the <a>UndoManager</a>.
  </p>
</div>
    </div>
    <!-- * * * * * * * * * * * * * -->
    <!--  End of GoJS sample code  -->
  </div>
</body>
<!--  This script is part of the gojs.net website, and is not needed to run the sample -->
<script src="../assets/js/goSamples.js"></script>
</html>
